package com.macro.mall.tiny.modules.pms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.modules.pms.mapper.PmsNavigationMapper;
import com.macro.mall.tiny.modules.pms.model.PmsNavigation;
import com.macro.mall.tiny.modules.pms.service.PmsNavigationService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 导航栏Service实现类
 */
@Service
public class PmsNavigationServiceImpl extends ServiceImpl<PmsNavigationMapper, PmsNavigation> implements PmsNavigationService {

    @Override
    public List<PmsNavigation> listVisible() {
        LambdaQueryWrapper<PmsNavigation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsNavigation::getHidden, 1)
                .orderByAsc(PmsNavigation::getSort);
        return list(wrapper);
    }

    @Override
    public Page<PmsNavigation> list(String keyword, Integer pageSize, Integer pageNum) {
        Page<PmsNavigation> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PmsNavigation> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like("name", keyword);
        }
        wrapper.orderByAsc("sort");
        return page(page, wrapper);
    }

    @Override
    public boolean create(PmsNavigation pmsNavigation) {
        pmsNavigation.setCreateTime(new Date());
        pmsNavigation.setUpdateTime(new Date());
        return save(pmsNavigation);
    }

    @Override
    public boolean update(Long id, PmsNavigation pmsNavigation) {
        pmsNavigation.setId(id);
        pmsNavigation.setUpdateTime(new Date());
        return updateById(pmsNavigation);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }
}
